package com.taotao.service.impl;

import com.taotao.common.EUTreeNode;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.service.TbItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TbItemCatServiceImpl implements TbItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    /**
     * 按照parentId查询商品类目
     * @param parentId
     * @return
     */
    @Override
    public List<EUTreeNode> getTbItemCatList(long parentId) {
        // 按照parentId查询分类数据
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(tbItemCatExample);

        List<EUTreeNode> euTreeNodeList = new ArrayList<>();
        // 把查询到的分类数据封装成EasyUI异步树数据结构
        for (TbItemCat tbItemCat : tbItemCatList){
            EUTreeNode euTreeNode = new EUTreeNode();
            euTreeNode.setId(tbItemCat.getId());
            euTreeNode.setState(tbItemCat.getIsParent() ? "closed" : "open");
            euTreeNode.setText(tbItemCat.getName());
            euTreeNodeList.add(euTreeNode);
        }
        return euTreeNodeList;
    }
}
